<script setup>
/**
 * @description Markdown渲染组件
 * @component MarkdownRenderer
 * @description 负责将markdown文本渲染为HTML，并提供统一的样式
 */

import { onMounted } from 'vue'
import MarkdownIt from 'markdown-it'

// 定义组件属性
const props = defineProps({
  content: {
    type: String,
    required: true,
    default: ''
  }
})

// 初始化markdown-it实例
const md = new MarkdownIt({
  html: true,
  breaks: true,
  linkify: true
})
</script>

<template>
  <div class="markdown-content" v-html="md.render(content)"></div>
</template>

<style scoped>
.markdown-content {
  line-height: 1.6;
  text-align: left;
}

.markdown-content :deep(p) {
  margin: 0.5em 0;
}

.markdown-content :deep(code) {
  background-color: rgba(0, 0, 0, 0.05);
  padding: 2px 4px;
  border-radius: 3px;
  font-family: monospace;
}

.markdown-content :deep(pre) {
  background-color: rgba(0, 0, 0, 0.05);
  padding: 1em;
  border-radius: 4px;
  overflow-x: auto;
}

.markdown-content :deep(a) {
  color: #409EFF;
  text-decoration: none;
}

.markdown-content :deep(a:hover) {
  text-decoration: underline;
}

.markdown-content :deep(ul), .markdown-content :deep(ol) {
  list-style: none;
  padding-left: 0;
  margin: 0.5em 0;
}

.markdown-content :deep(ul) li, .markdown-content :deep(ol) li {
  position: relative;
  padding-left: 1.5em;
  margin: 0.3em 0;
}

.markdown-content :deep(ul) li::before {
  content: '';
  position: absolute;
  left: 0;
  top: 0.6em;
  width: 6px;
  height: 6px;
  background-color: #409EFF;
  border-radius: 50%;
}

.markdown-content :deep(ol) {
  counter-reset: item;
}

.markdown-content :deep(ol) li::before {
  content: counter(item) '.';
  counter-increment: item;
  position: absolute;
  left: 0;
  color: #409EFF;
  font-weight: 500;
}

.markdown-content :deep(h1), 
.markdown-content :deep(h2), 
.markdown-content :deep(h3), 
.markdown-content :deep(h4), 
.markdown-content :deep(h5), 
.markdown-content :deep(h6) {
  margin: 1em 0 0.5em;
  line-height: 1.4;
}

.markdown-content :deep(blockquote) {
  margin: 0.5em 0;
  padding: 0.5em 1em;
  border-left: 4px solid #409EFF;
  background-color: rgba(0, 0, 0, 0.03);
}

.markdown-content :deep(table) {
  border-collapse: collapse;
  width: 100%;
  margin: 0.5em 0;
}

.markdown-content :deep(th),
.markdown-content :deep(td) {
  border: 1px solid #dcdfe6;
  padding: 8px;
  text-align: left;
}

.markdown-content :deep(th) {
  background-color: #f5f7fa;
}

.markdown-content :deep(img) {
  max-width: 100%;
  height: auto;
}
</style>